//
//  TimePickView.m
//  WashCar
//
//  Created by 就是这么自信 on 16/4/8.
//  Copyright © 2016年 张越. All rights reserved.
//

#import "TimePickView.h"
#define PickViewWidth 250.0f
#define PickViewHeight 380.0f
@implementation TimePickView

+(TimePickView *)instancePickView
{
    NSArray* nibView =  [[NSBundle mainBundle] loadNibNamed:@"TimePickView" owner:nil options:nil];
    return [nibView objectAtIndex:0];
}

-(id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if(self)
    {
        //you init
    }
    return self;
}

-(void)configWithLayers
{
    UIWindow *shareWindow = [[UIApplication sharedApplication]keyWindow];
    if (!self.backImageView) {
        self.backImageView = [[UIView alloc] initWithFrame:shareWindow.bounds];
    }
    self.backImageView.backgroundColor = [UIColor blackColor];
    self.backImageView.alpha = 0.6f;
    [shareWindow addSubview:self.backImageView];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = self.backImageView.frame;
    btn.userInteractionEnabled = YES;
    btn.backgroundColor = [UIColor clearColor];
    [btn addTarget:self action:@selector(cancelBtn_Touch:) forControlEvents:UIControlEventTouchUpInside];
    [self.backImageView addSubview:btn];
    
    self.starBackView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.starBackView.layer.borderWidth = 0.5;
    self.starBackView.layer.cornerRadius = 5;
    self.startPickView.delegate = self;
    self.startPickView.dataSource = self;
    self.startPickView.showsSelectionIndicator = YES;
    
    self.TimeBackView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.TimeBackView.layer.borderWidth = 0.5;
    self.TimeBackView.layer.cornerRadius = 5;
    self.TimePickView.delegate = self;
    self.TimePickView.dataSource = self;
    self.TimePickView.showsSelectionIndicator = YES;
    self.yearArray = [NSMutableArray array];
    NSDate *senddate = [NSDate date];
    NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:@"yyyy"];
    NSString *locationString=[dateformatter stringFromDate:senddate];
    for (int i = 0; i < 20; i++) {
        [self.yearArray addObject:[NSString stringWithFormat:@"%ld年",(long)[locationString integerValue]+i]];
    }
    self.motehArray = [[NSMutableArray alloc]initWithObjects:@"01月",@"02月",@"03月",@"04月",@"05月",@"06月",@"07月",@"08月",@"09月",@"10月",@"11月",@"12月",nil];
    self.dateArray = [[NSMutableArray alloc]initWithObjects:@"01日",@"02日",@"03日",@"04日",@"05日",@"06日",@"07日",@"08日",@"09日",@"10日",@"11日",@"12日",@"13日",@"14日",@"15日",@"16日",@"17日",@"18日",@"19日",@"20日",@"21日",@"22日",@"23日",@"24日",@"25日",@"26日",@"27日",@"28日",@"29日",@"30日",@"31日",nil];
    
    self.timeAyy = [[NSMutableArray alloc]initWithObjects:@"01时",@"02时",@"03时",@"04时",@"05时",@"06时",@"07时",@"08时",@"09时",@"10时",@"11时",@"12时",nil];
    self.branchArray = [[NSMutableArray alloc]initWithObjects:@"00分",@"01分",@"02分",@"03分",@"04分",@"05分",@"06分",@"07分",@"08分",@"09分",@"10分",@"11分",@"12分",@"13分",@"14分",@"15分",@"16分",@"17分",@"18分",@"19分",@"20分",@"21分",@"22分",@"23分",@"24分",@"25分",@"26分",@"27分",@"28分",@"29分",@"30分",@"31分",@"32分",@"33分",@"34分",@"35分",@"36分",@"37分",@"38分",@"39分",@"40分",@"41分",@"42分",@"43分",@"44分",@"45分",@"46分",@"47分",@"48分",@"49分",@"50分",@"51分",@"52分",@"53分",@"54分",@"55分",@"56分",@"57分",@"58分",@"59分",nil];
    self.statusArray = [[NSMutableArray alloc]initWithObjects:@"上午",@"下午",nil];
    year = self.yearArray[0];
    month = self.motehArray[0];
    day = self.dateArray[0];
    time = self.timeAyy[0];
    branch = self.branchArray[0];
    status = self.statusArray[0];
    CGRect afterFrame = CGRectMake((CGRectGetWidth(shareWindow.bounds) -PickViewWidth) * 0.5, (CGRectGetHeight(shareWindow.bounds) - PickViewHeight) * 0.5, PickViewWidth, PickViewHeight);
    [UIView animateWithDuration:0.35f delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
    } completion:^(BOOL finished) {
        self.frame = afterFrame;
    }];
}

- (IBAction)cancelBtn_Touch:(id)sender {
    [self removeFromSuperview];
    if (self.cancelBlock) {
        self.cancelBlock();
    }
}

- (IBAction)throwBtn_Touch:(id)sender {
    [self removeFromSuperview];
    if (self.ReturnStringBlock) {
        NSString * years = [year substringToIndex:4];
        NSString * months = [month substringToIndex:2];
        NSString * days = [day substringToIndex:2];
        NSString * times = [time substringToIndex:2];
        NSString * branchs = [branch substringToIndex:2];
        self.ReturnStringBlock(years,months,days,times,branchs,status);
    }
}

#pragma - mark PickViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 3;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if(pickerView == self.startPickView)
    {
        if (component == 0) {
            return [self.yearArray count];
        }else if(component == 1){
            return [self.motehArray count];
        }else{
            return [self.dateArray count];
        }
    }else{
        if (component == 0) {
            return [self.timeAyy count];
        }else if(component == 1){
            return [self.branchArray count];
        }else{
            return [self.statusArray count];
        }
    }
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    return 60;
}

#pragma - mark PickViewDeleGate
//返回当前行的内容,此处是将数组中数值添加到滚动的那个显示栏上
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if(pickerView == self.startPickView)
    {
        if (component == 0) {
            return [self.yearArray objectAtIndex:row];
        } else  if(component == 1){
            return [self.motehArray objectAtIndex:row];
        }else{
            return [self.dateArray objectAtIndex:row];
        }
    }else{
        if (component == 0) {
            return [self.timeAyy objectAtIndex:row];
        } else  if(component == 1){
            return [self.branchArray objectAtIndex:row];
        }else{
            return [self.statusArray objectAtIndex:row];
        }
    }
}

// 返回选中的行
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if(pickerView == self.startPickView)
    {
        if (component == 0) {
            year = self.yearArray[row];
            NSString * years = [year substringToIndex:4];

            if(([years integerValue] % 400 == 0 || ([years integerValue] % 4 == 0 && [years integerValue] % 100 != 0)) && [month isEqualToString:@"02月"])
            {
                self.dateArray = [[NSMutableArray alloc]initWithObjects:@"01日",@"02日",@"03日",@"04日",@"05日",@"06日",@"07日",@"08日",@"09日",@"10日",@"11日",@"12日",@"13日",@"14日",@"15日",@"16日",@"17日",@"18日",@"19日",@"20日",@"21日",@"22日",@"23日",@"24日",@"25日",@"26日",@"27日",@"28日",@"29日",nil];
            }else{
                self.dateArray = [[NSMutableArray alloc]initWithObjects:@"01日",@"02日",@"03日",@"04日",@"05日",@"06日",@"07日",@"08日",@"09日",@"10日",@"11日",@"12日",@"13日",@"14日",@"15日",@"16日",@"17日",@"18日",@"19日",@"20日",@"21日",@"22日",@"23日",@"24日",@"25日",@"26日",@"27日",@"28日",nil];
            }
            [pickerView reloadAllComponents];
        } else if(component == 1){
            if(row==0 || row == 2 || row == 4 || row == 6 || row == 7 || row == 9 || row == 11)
            {
                self.dateArray = [[NSMutableArray alloc]initWithObjects:@"01日",@"02日",@"03日",@"04日",@"05日",@"06日",@"07日",@"08日",@"09日",@"10日",@"11日",@"12日",@"13日",@"14日",@"15日",@"16日",@"17日",@"18日",@"19日",@"20日",@"21日",@"22日",@"23日",@"24日",@"25日",@"26日",@"27日",@"28日",@"29日",@"30日",@"31日",nil];
            }else if(row == 1){
                NSString * years = [year substringToIndex:4];
                if ([years integerValue] % 400 == 0 || ([years integerValue] % 4 == 0 && [years integerValue] % 100 != 0)) {
                    self.dateArray = [[NSMutableArray alloc]initWithObjects:@"01日",@"02日",@"03日",@"04日",@"05日",@"06日",@"07日",@"08日",@"09日",@"10日",@"11日",@"12日",@"13日",@"14日",@"15日",@"16日",@"17日",@"18日",@"19日",@"20日",@"21日",@"22日",@"23日",@"24日",@"25日",@"26日",@"27日",@"28日",@"29日",nil];
                }else{
                    self.dateArray = [[NSMutableArray alloc]initWithObjects:@"01日",@"02日",@"03日",@"04日",@"05日",@"06日",@"07日",@"08日",@"09日",@"10日",@"11日",@"12日",@"13日",@"14日",@"15日",@"16日",@"17日",@"18日",@"19日",@"20日",@"21日",@"22日",@"23日",@"24日",@"25日",@"26日",@"27日",@"28日",nil];
                }
            }else{
                self.dateArray = [[NSMutableArray alloc]initWithObjects:@"01日",@"02日",@"03日",@"04日",@"05日",@"06日",@"07日",@"08日",@"09日",@"10日",@"11日",@"12日",@"13日",@"14日",@"15日",@"16日",@"17日",@"18日",@"19日",@"20日",@"21日",@"22日",@"23日",@"24日",@"25日",@"26日",@"27日",@"28日",@"29日",@"30日",nil];
            }
            month = self.motehArray[row];
            [pickerView reloadAllComponents];
        }else{
            day = self.dateArray[row];
        }
    }else{
        if(component == 0)
        {
            time = self.timeAyy[row];
        }else if (component == 1)
        {
            branch = self.branchArray[row];
        }else{
            status = self.statusArray[row];
        }
    }
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    UILabel* pickerLabel = (UILabel*)view;
    if (!pickerLabel){
        pickerLabel = [[UILabel alloc] init];
        pickerLabel.minimumScaleFactor = 8.0;
        pickerLabel.adjustsFontSizeToFitWidth = YES;
        [pickerLabel setTextAlignment:NSTextAlignmentCenter];
        [pickerLabel setBackgroundColor:[UIColor clearColor]];
        [pickerLabel setFont:[UIFont systemFontOfSize:13]];
    }
    pickerLabel.text=[self pickerView:pickerView titleForRow:row forComponent:component];
    return pickerLabel;
}

-(void)show
{
    UIWindow *shareWindow = [[UIApplication sharedApplication]keyWindow];
    [shareWindow addSubview:self];
}

-(void)removeFromSuperview
{
    [self.backImageView removeFromSuperview];
    self.backImageView = nil;
    UIWindow *shareWindow = [[UIApplication sharedApplication]keyWindow];
    if (ScreenWidth == 320) {
        CGRect afterFrame = CGRectMake((CGRectGetWidth(shareWindow.bounds) - PickViewWidth) * 0.5, CGRectGetHeight(shareWindow.bounds), PickViewWidth * 0.8, PickViewHeight);
        [UIView animateWithDuration:0.45f delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
            self.frame = afterFrame;
        } completion:^(BOOL finished) {
            [super removeFromSuperview];
        }];
    } else {
        CGRect afterFrame = CGRectMake((CGRectGetWidth(shareWindow.bounds) - PickViewWidth) * 0.5, CGRectGetHeight(shareWindow.bounds), PickViewWidth * 0.95, PickViewHeight);
        [UIView animateWithDuration:0.45f delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
            self.frame = afterFrame;
        } completion:^(BOOL finished) {
            [super removeFromSuperview];
        }];
    }
}

@end
